package com.jwell.panrank.ui.activity

import android.opengl.Visibility
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.view.WindowManager
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.jwell.cargoapp.ui.base.MVPBaseActivity
import com.jwell.panrank.R
import com.jwell.panrank.application.App
import com.jwell.panrank.contract.WaybillContract
import com.jwell.panrank.entity.UnLoadingPointBean
import com.jwell.panrank.entity.WaybillBean
import com.jwell.panrank.model.WaybillModel
import com.jwell.panrank.presenter.WaybillPresenter
import com.jwell.panrank.tools.DataModel
import com.jwell.panrank.ui.adapter.WaybillAdapter
import com.jwell.panrank.ui.custom.DateChooseWheelViewDialog
import com.jwell.panrank.ui.dialog.ChooseStringDialog
import kotlinx.android.synthetic.main.activity_waybill_search.*
import kotlinx.android.synthetic.main.empty_layout.*
import kotlinx.android.synthetic.main.error_layout.*
import kotlinx.android.synthetic.main.list_layout.*
import kotlinx.android.synthetic.main.toolbar.*

class WaybillSearchActivity : MVPBaseActivity<WaybillPresenter>(), WaybillContract.View, View.OnClickListener {


    private var haveSearch = false
    private var searchData = WaybillModel.RequestData()
    private var planId = 0
    private var listData = ArrayList<WaybillBean>()
    private var adapter = WaybillAdapter(listData)

    private var measurementTypeList = mutableListOf<Int>()
    private var billStateList = mutableListOf<Int>()

    override fun createPresenter(): WaybillPresenter? {
        return WaybillPresenter()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
        setContentView(R.layout.activity_waybill_search)
        initToolBar("运单搜索", true, null)
        toolbar_right_text_save.text = "搜索"
        toolbar_right_text_save.visibility = View.VISIBLE
        setListener()
        initData()
    }

    override fun setSearchTotalCount(count: Int) {
        if (count > 0) {
            initToolBar("搜索到" + count + "条运单", true, null)
        } else {
            initToolBar("未搜索到相关运单", true, null)
        }

    }

    private fun setListener() {
        text_time1.setOnClickListener(this)
        text_time2.setOnClickListener(this)
        text_time3.setOnClickListener(this)
        text_time4.setOnClickListener(this)
        text_time5.setOnClickListener(this)
        text_time6.setOnClickListener(this)
        text_time7.setOnClickListener(this)
        text_time8.setOnClickListener(this)
        toolbar_right_text_save.setOnClickListener(this)
        rl_item11.setOnClickListener(this)
        rl_item12.setOnClickListener(this)
        rl_item13.setOnClickListener(this)
        rl_item14.setOnClickListener(this)
        rl_item15.setOnClickListener(this)
        rl_item16.setOnClickListener(this)
        rl_item31.setOnClickListener(this)
        rl_item32.setOnClickListener(this)
        rl_item33.setOnClickListener(this)
        rl_item34.setOnClickListener(this)
        rl_item35.setOnClickListener(this)
        rl_item36.setOnClickListener(this)
        image_delete.setOnClickListener(this)
        edit_receiptAddress.setOnClickListener(this)

    }

    private fun setLabel(c1: Boolean, rl_item: RelativeLayout, imageView: ImageView, textView: TextView) {
        rl_item.isSelected = c1
        setTextColor(c1, textView, imageView)
        when (rl_item) {
            rl_item31 -> {
                if (c1) {
                    measurementTypeList.add(3)
                } else {
                    measurementTypeList.remove(3)
                }
            }
            rl_item32 -> {
                if (c1) {
                    measurementTypeList.add(1)
                } else {
                    measurementTypeList.remove(1)
                }
            }
            rl_item33 -> {
                if (c1) {
                    measurementTypeList.add(2)
                } else {
                    measurementTypeList.remove(2)
                }
            }
            rl_item34 -> {
                if (c1) {
                    measurementTypeList.add(4)
                } else {
                    measurementTypeList.remove(4)
                }
            }
            rl_item35 -> {
                if (c1) {
                    measurementTypeList.add(5)
                } else {
                    measurementTypeList.remove(5)
                }
            }
            rl_item36 -> {
                if (c1) {
                    measurementTypeList.add(6)
                } else {
                    measurementTypeList.remove(6)
                }
            }
            rl_item11 -> {
                if (c1) {
                    billStateList.add(1)
                } else {
                    billStateList.remove(1)
                }
            }
            rl_item12 -> {
                if (c1) {
                    billStateList.add(2)
                } else {
                    billStateList.remove(2)
                }
            }
            rl_item13 -> {
                if (c1) {
                    billStateList.add(3)
                } else {
                    billStateList.remove(3)
                }
            }
            rl_item14 -> {
                if (c1) {
                    billStateList.add(4)
                } else {
                    billStateList.remove(4)
                }
            }
            rl_item15 -> {
                if (c1) {
                    billStateList.add(5)
                } else {
                    billStateList.remove(5)
                }
            }
            rl_item16 -> {
                if (c1) {
                    billStateList.add(9)
                } else {
                    billStateList.remove(9)
                }
            }
        }
    }

    private fun setTextColor(isChecked: Boolean, textView: TextView, imageView: ImageView) {
        when (isChecked) {
            true -> {
                textView.setTextColor(application.resources.getColor(R.color.black))
                imageView.visibility = View.VISIBLE
            }
            false -> {
                textView.setTextColor(application.resources.getColor(R.color.c999999))
                imageView.visibility = View.GONE
            }
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.image_delete -> {
                edit_receiptAddress.text = null
                setDeteteImageVisibilty(false)
            }
        //选择收料点
            R.id.edit_receiptAddress -> {
                var unLoadingPintList: List<UnLoadingPointBean>? = null
                DataModel.getInstance().unloadingPoint?.let {
                    unLoadingPintList = it
                }
                var stringList = mutableListOf<String>()
                unLoadingPintList?.let {
                    it.mapTo(stringList) { it.point }
                }
                if (stringList.isEmpty()) {
                    toast("未获取到卸料点信息")
                    return
                }
                var dialog1 = ChooseStringDialog.Builder(this, stringList)
                        .setOnclickListener { text ->
                            edit_receiptAddress.text = text
                            if (edit_receiptAddress.text.isNullOrEmpty()) {
                                setDeteteImageVisibilty(false)
                            } else {
                                setDeteteImageVisibilty(true)
                            }
                        }
                        .creat()
                dialog1.show()
            }
            R.id.text_time1 -> {
                val startDateChooseDialog = DateChooseWheelViewDialog(this@WaybillSearchActivity, DateChooseWheelViewDialog.DateChooseInterface { time, longTimeChecked ->
                    text_time1.text = time
                })
                startDateChooseDialog.setDateDialogTitle("创建开始时间")
                startDateChooseDialog.setTimePickerGone(true)
                startDateChooseDialog.showDateChooseDialog()
            }
            R.id.text_time2 -> {
                val startDateChooseDialog = DateChooseWheelViewDialog(this@WaybillSearchActivity, DateChooseWheelViewDialog.DateChooseInterface { time, longTimeChecked ->
                    text_time2.text = time
                })
                startDateChooseDialog.setDateDialogTitle("创建结束时间")
                startDateChooseDialog.setTimePickerGone(true)
                startDateChooseDialog.showDateChooseDialog()
            }
            R.id.text_time3 -> {
                val startDateChooseDialog = DateChooseWheelViewDialog(this@WaybillSearchActivity, DateChooseWheelViewDialog.DateChooseInterface { time, longTimeChecked ->
                    text_time3.text = time
                })
                startDateChooseDialog.setDateDialogTitle("供应开始时间")
                startDateChooseDialog.setTimePickerGone(true)
                startDateChooseDialog.showDateChooseDialog()
            }
            R.id.text_time4 -> {
                val startDateChooseDialog = DateChooseWheelViewDialog(this@WaybillSearchActivity, DateChooseWheelViewDialog.DateChooseInterface { time, longTimeChecked ->
                    text_time4.text = time
                })
                startDateChooseDialog.setDateDialogTitle("供应结束时间")
                startDateChooseDialog.setTimePickerGone(true)
                startDateChooseDialog.showDateChooseDialog()
            }
            R.id.text_time5 -> {
                val startDateChooseDialog = DateChooseWheelViewDialog(this@WaybillSearchActivity, DateChooseWheelViewDialog.DateChooseInterface { time, longTimeChecked ->
                    text_time5.text = time
                })
                startDateChooseDialog.setDateDialogTitle("收料开始时间")
                startDateChooseDialog.setTimePickerGone(false)
                startDateChooseDialog.showDateChooseDialog()
            }
            R.id.text_time6 -> {
                val startDateChooseDialog = DateChooseWheelViewDialog(this@WaybillSearchActivity, DateChooseWheelViewDialog.DateChooseInterface { time, longTimeChecked ->
                    text_time6.text = time
                })
                startDateChooseDialog.setDateDialogTitle("收料结束时间")
                startDateChooseDialog.setTimePickerGone(false)
                startDateChooseDialog.showDateChooseDialog()
            }
            R.id.text_time7 -> {
                val startDateChooseDialog = DateChooseWheelViewDialog(this@WaybillSearchActivity, DateChooseWheelViewDialog.DateChooseInterface { time, longTimeChecked ->
                    text_time7.text = time
                })
                startDateChooseDialog.setDateDialogTitle("取样开始时间")
                startDateChooseDialog.setTimePickerGone(false)
                startDateChooseDialog.showDateChooseDialog()
            }
            R.id.text_time8 -> {
                val startDateChooseDialog = DateChooseWheelViewDialog(this@WaybillSearchActivity, DateChooseWheelViewDialog.DateChooseInterface { time, longTimeChecked ->
                    text_time8.text = time
                })
                startDateChooseDialog.setDateDialogTitle("取样结束时间")
                startDateChooseDialog.setTimePickerGone(false)
                startDateChooseDialog.showDateChooseDialog()
            }
            R.id.toolbar_right_text_save -> {
                when (toolbar_right_text_save.text) {
                    "重新搜索" -> {
                        list_layout.visibility = View.GONE
                        scrollView_search.visibility = View.VISIBLE
                        haveSearch = false
                    }
                    "搜索" -> {
                        searchData.measurementTypeList = measurementTypeList
                        searchData.billStateList = billStateList
                        searchData.planCode = edit_jhCode.text.toString()
                        searchData.forBillCode = edit_waybillCode.text.toString()
                        searchData.supplierName = edit_carrierName.text.toString()
                        searchData.forwardingUnit = edit_sendCom.text.toString()
                        searchData.receivingUnit = edit_receiptCom.text.toString()
                        searchData.mtrlOrderNo = edit_zhixingdanhanghao.text.toString()
                        searchData.materialName = edit_goodsName.text.toString()
                        searchData.driverName = edit_driverName.text.toString()
                        searchData.carNum = edit_carNum.text.toString()
                        searchData.createUserName = edit_createPerson.text.toString()
                        searchData.unloadPoint = edit_receiptAddress.text.toString()
                        searchData.stateDate = text_time1.text.toString()
                        searchData.endDate = text_time2.text.toString()
                        searchData.planStartDate = text_time3.text.toString()
                        searchData.planEndDate = text_time4.text.toString()
                        searchData.feedingStartTime = text_time5.text.toString()
                        searchData.feedingEndTime = text_time6.text.toString()
                        searchData.samplingStartTime = text_time7.text.toString()
                        searchData.samplingEndTime = text_time8.text.toString()
                        searchData.remark = edit_remark.text.toString()
                        try {
                            searchData.waitTime = edit_waitTime.text.toString().toInt()
                        } catch (e: Exception) {
                            e.printStackTrace()
                            searchData.waitTime = null
                        }
                        searchData.skipCount = 0
                        searchData.planId = planId
                        showProgress("", true)
                        DataModel.getInstance().saveWaybillSearchData(searchData)
                        presenter?.searchListData(searchData)
                        haveSearch = true
                        list_layout.visibility = View.VISIBLE
                        scrollView_search.visibility = View.GONE
                    }
                }
                when (haveSearch) {
                    true -> {
                        toolbar_right_text_save.text = "重新搜索"
                    }
                    false -> {
                        toolbar_right_text_save.text = "搜索"
                        initToolBar("运单搜索", true, null)
                    }
                }
            }
            R.id.rl_item11 -> setLabel(!rl_item11.isSelected, rl_item11, image_tick_11, text_label11)
            R.id.rl_item12 -> setLabel(!rl_item12.isSelected, rl_item12, image_tick_12, text_label12)
            R.id.rl_item13 -> setLabel(!rl_item13.isSelected, rl_item13, image_tick_13, text_label13)
            R.id.rl_item14 -> setLabel(!rl_item14.isSelected, rl_item14, image_tick_14, text_label14)
            R.id.rl_item15 -> setLabel(!rl_item15.isSelected, rl_item15, image_tick_15, text_label15)
            R.id.rl_item16 -> setLabel(!rl_item16.isSelected, rl_item16, image_tick_16, text_label16)
            R.id.rl_item31 -> setLabel(!rl_item31.isSelected, rl_item31, image_tick_31, text_label31)
            R.id.rl_item32 -> setLabel(!rl_item32.isSelected, rl_item32, image_tick_32, text_label32)
            R.id.rl_item33 -> setLabel(!rl_item33.isSelected, rl_item33, image_tick_33, text_label33)
            R.id.rl_item34 -> setLabel(!rl_item34.isSelected, rl_item34, image_tick_34, text_label34)
            R.id.rl_item35 -> setLabel(!rl_item35.isSelected, rl_item35, image_tick_35, text_label35)
            R.id.rl_item36 -> setLabel(!rl_item36.isSelected, rl_item36, image_tick_36, text_label36)
        }
    }

    private fun initData() {
        intent?.let {
            planId = it.getIntExtra("id", 0)
        }
        initEmptyAndErrorView()
        loadingLayout.visibility = View.GONE
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = adapter
        freshLayout.setEnableLoadmoreWhenContentNotFull(true)
        freshLayout.setEnableFooterFollowWhenLoadFinished(true)
        freshLayout.setOnRefreshListener {
            presenter?.fresh()
        }
        freshLayout.setOnLoadmoreListener {
            presenter?.load()
        }
        DataModel.getInstance().waybillSearchData?.let {
            it.billStateList?.let {
                if (it.contains(1)) {
                    setLabel(!rl_item11.isSelected, rl_item11, image_tick_11, text_label11)
                }
                if (it.contains(2)) {
                    setLabel(!rl_item12.isSelected, rl_item12, image_tick_12, text_label12)
                }
                if (it.contains(3)) {
                    setLabel(!rl_item13.isSelected, rl_item13, image_tick_13, text_label13)
                }
                if (it.contains(4)) {
                    setLabel(!rl_item14.isSelected, rl_item14, image_tick_14, text_label14)
                }
                if (it.contains(5)) {
                    setLabel(!rl_item15.isSelected, rl_item15, image_tick_15, text_label15)
                }
                if (it.contains(9)) {
                    setLabel(!rl_item16.isSelected, rl_item16, image_tick_16, text_label16)
                }
            }
            it.measurementTypeList?.let {
                if (it.contains(3)) {
                    setLabel(!rl_item31.isSelected, rl_item31, image_tick_31, text_label31)
                }
                if (it.contains(1)) {
                    setLabel(!rl_item32.isSelected, rl_item32, image_tick_32, text_label32)
                }
                if (it.contains(2)) {
                    setLabel(!rl_item33.isSelected, rl_item33, image_tick_33, text_label33)
                }
                if (it.contains(4)) {
                    setLabel(!rl_item34.isSelected, rl_item34, image_tick_34, text_label34)
                }
                if (it.contains(5)) {
                    setLabel(!rl_item35.isSelected, rl_item35, image_tick_35, text_label35)
                }
                if (it.contains(6)) {
                    setLabel(!rl_item36.isSelected, rl_item36, image_tick_36, text_label36)
                }
            }
            edit_jhCode.setText(it.planCode)
            edit_waybillCode.setText(it.forBillCode)
            edit_receiptCom.setText(it.receivingUnit)
            edit_zhixingdanhanghao.setText(it.mtrlOrderNo)
            edit_goodsName.setText(it.materialName)
            edit_driverName.setText(it.driverName)
            edit_carNum.setText(it.carNum)
            edit_carrierName.setText(it.supplierName)
            edit_sendCom.setText(it.forwardingUnit)
            edit_createPerson.setText(it.createUserName)
            it.waitTime?.let {
                edit_waitTime.setText(it.toString())
            }
            edit_receiptAddress.text = it.unloadPoint
        }
        if (edit_receiptAddress.text.isNullOrEmpty()) {
            setDeteteImageVisibilty(false)
        } else {
            setDeteteImageVisibilty(true)
        }
    }

    fun setDeteteImageVisibilty(visibility: Boolean) {
        if (visibility) {
            image_delete.visibility = View.VISIBLE
        } else {
            image_delete.visibility = View.GONE
        }
    }

    private fun initEmptyAndErrorView() {
        image_empty.setImageResource(R.mipmap.icon_waybill)
        text_empty_describe.text = "无搜索到符合条件的运单"
        ll_error.setOnClickListener {
            errorLayout.visibility = View.GONE
            loadingLayout.visibility = View.VISIBLE
            presenter?.fresh()
        }
    }

    override fun showListData(data: List<WaybillBean>, isFresh: Boolean) {
        loadingLayout.visibility = View.GONE
        errorLayout.visibility = View.GONE
        emptyLayout.visibility = View.GONE
        if (isFresh) {
            listData.clear()
            freshLayout.finishRefresh()
            //设置可以加载更多
            freshLayout.resetNoMoreData()
            if (App.DEBUG) println("设置可以加载更多")
        } else {
            freshLayout.finishLoadmore()
        }
        listData.addAll(data)
        adapter.notifyDataSetChanged()
        if (listData.size == 0) {
            emptyLayout.visibility = View.VISIBLE
        } else if (data.size < 10) {
            freshLayout.finishLoadmoreWithNoMoreData()
            if (App.DEBUG) println("设置不可以加载更多")
        }
    }

    override fun showListDataFailed(isFresh: Boolean, str: String) {
        loadingLayout.visibility = View.GONE
        errorLayout.visibility = View.GONE
        emptyLayout.visibility = View.GONE
        if (isFresh) {
            freshLayout.finishRefresh(false)
            if (listData.size == 0) errorLayout.visibility = View.VISIBLE
            freshLayout.finishLoadmore(false)
        } else {
            freshLayout.finishLoadmore(false)
        }
    }
}
